<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>test.sol</title>
<meta name="generator" content="KF5::SyntaxHighlighting - Definition (Solidity) - Theme (Breeze Dark)"/>
</head><body style="background-color:#232629;color:#cfcfc2"><pre>
<span style="color:#7a7c7d;">// Solidity Sample File</span>
<span style="color:#7a7c7d;">// https://solidity.readthedocs.io/en/latest/solidity-by-example.html#voting</span>

<span style="font-weight:bold;">pragma</span> <span style="color:#7f8c8d;">solidity</span> &gt;=0.4.22 &lt;0.7.0;

<span style="color:#7a7c7d;">/// </span><span style="color:#3f8058;">@title</span><span style="color:#7a7c7d;"> Voting with delegation.</span>
<span style="color:#fdbc4b;font-weight:bold;">contract</span> Ballot {
    <span style="color:#7a7c7d;">// This declares a new complex type which will</span>
    <span style="color:#7a7c7d;">// be used for variables later.</span>
    <span style="color:#7a7c7d;">// It will represent a single voter.</span>
    <span style="font-weight:bold;">struct</span> Voter {
        <span style="color:#2980b9;">uint</span> weight; <span style="color:#7a7c7d;">// weight is accumulated by delegation</span>
        <span style="color:#2980b9;">bool</span> voted;  <span style="color:#7a7c7d;">// if true, that person already voted</span>
        <span style="color:#2980b9;">address</span> delegate; <span style="color:#7a7c7d;">// person delegated to</span>
        <span style="color:#2980b9;">uint</span> vote;   <span style="color:#7a7c7d;">// index of the voted proposal</span>
    }

    <span style="color:#7a7c7d;">// This is a type for a single proposal.</span>
    <span style="font-weight:bold;">struct</span> Proposal {
        <span style="color:#2980b9;">bytes32</span> name;   <span style="color:#7a7c7d;">// short name (up to 32 bytes)</span>
        <span style="color:#2980b9;">uint</span> voteCount; <span style="color:#7a7c7d;">// number of accumulated votes</span>
    }

    <span style="color:#2980b9;">address</span> <span style="font-weight:bold;">public</span> chairperson;

    <span style="color:#7a7c7d;">// This declares a state variable that</span>
    <span style="color:#7a7c7d;">// stores a `Voter` struct for each possible address.</span>
    <span style="color:#2980b9;">mapping</span>(<span style="color:#2980b9;">address</span> =&gt; Voter) <span style="font-weight:bold;">public</span> voters;

    <span style="color:#7a7c7d;">// A dynamically-sized array of `Proposal` structs.</span>
    Proposal[] <span style="font-weight:bold;">public</span> proposals;

    <span style="color:#7a7c7d;">/// Create a new ballot to choose one of `proposalNames`.</span>
    <span style="color:#fdbc4b;font-weight:bold;">constructor</span>(<span style="color:#2980b9;">bytes32</span>[] <span style="font-weight:bold;">memory</span> proposalNames) <span style="font-weight:bold;">public</span> {
        chairperson = <span style="color:#7f8c8d;">msg</span>.sender;
        voters[chairperson].weight = <span style="color:#f67400;">1</span>;

        <span style="color:#7a7c7d;">// For each of the provided proposal names,</span>
        <span style="color:#7a7c7d;">// create a new proposal object and add it</span>
        <span style="color:#7a7c7d;">// to the end of the array.</span>
        <span style="color:#fdbc4b;font-weight:bold;">for</span> (<span style="color:#2980b9;">uint</span> i = <span style="color:#f67400;">0</span>; i &lt; proposalNames.length; i<span style="color:#7f8c8d;">++</span>) {
            <span style="color:#7a7c7d;">// `Proposal({...})` creates a temporary</span>
            <span style="color:#7a7c7d;">// Proposal object and `proposals.push(...)`</span>
            <span style="color:#7a7c7d;">// appends it to the end of `proposals`.</span>
            proposals.push(Proposal({
                name: proposalNames[i],
                voteCount: <span style="color:#f67400;">0</span>
            }));
        }
    }

    <span style="color:#7a7c7d;">// Give `voter` the right to vote on this ballot.</span>
    <span style="color:#7a7c7d;">// May only be called by `chairperson`.</span>
    <span style="color:#fdbc4b;font-weight:bold;">function</span> giveRightToVote(<span style="color:#2980b9;">address</span> voter) <span style="font-weight:bold;">public</span> {
        <span style="color:#7a7c7d;">// If the first argument of `require` evaluates</span>
        <span style="color:#7a7c7d;">// to `false`, execution terminates and all</span>
        <span style="color:#7a7c7d;">// changes to the state and to Ether balances</span>
        <span style="color:#7a7c7d;">// are reverted.</span>
        <span style="color:#7a7c7d;">// This used to consume all gas in old EVM versions, but</span>
        <span style="color:#7a7c7d;">// not anymore.</span>
        <span style="color:#7a7c7d;">// It is often a good idea to use `require` to check if</span>
        <span style="color:#7a7c7d;">// functions are called correctly.</span>
        <span style="color:#7a7c7d;">// As a second argument, you can also provide an</span>
        <span style="color:#7a7c7d;">// explanation about what went wrong.</span>
        <span style="color:#7f8c8d;">require</span>(
            <span style="color:#7f8c8d;">msg</span>.sender == chairperson,
            <span style="color:#f44f4f;">&quot;Only chairperson can give right to vote.&quot;</span>
        );
        <span style="color:#7f8c8d;">require</span>(
            !voters[voter].voted,
            <span style="color:#f44f4f;">&quot;The voter already voted.&quot;</span>
        );
        <span style="color:#7f8c8d;">require</span>(voters[voter].weight == <span style="color:#f67400;">0</span>);
        voters[voter].weight = <span style="color:#f67400;">1</span>;
    }

    <span style="color:#7a7c7d;">/// Delegate your vote to the voter `to`.</span>
    <span style="color:#fdbc4b;font-weight:bold;">function</span> delegate(<span style="color:#2980b9;">address</span> to) <span style="font-weight:bold;">public</span> {
        <span style="color:#7a7c7d;">// assigns reference</span>
        Voter <span style="font-weight:bold;">storage</span> sender = voters[<span style="color:#7f8c8d;">msg</span>.sender];
        <span style="color:#7f8c8d;">require</span>(!sender.voted, <span style="color:#f44f4f;">&quot;You already voted.&quot;</span>);

        <span style="color:#7f8c8d;">require</span>(to != <span style="color:#7f8c8d;">msg</span>.sender, <span style="color:#f44f4f;">&quot;Self-delegation is disallowed.&quot;</span>);

        <span style="color:#7a7c7d;">// Forward the delegation as long as</span>
        <span style="color:#7a7c7d;">// `to` also delegated.</span>
        <span style="color:#7a7c7d;">// In general, such loops are very dangerous,</span>
        <span style="color:#7a7c7d;">// because if they run too long, they might</span>
        <span style="color:#7a7c7d;">// need more gas than is available in a block.</span>
        <span style="color:#7a7c7d;">// In this case, the delegation will not be executed,</span>
        <span style="color:#7a7c7d;">// but in other situations, such loops might</span>
        <span style="color:#7a7c7d;">// cause a contract to get &quot;stuck&quot; completely.</span>
        <span style="color:#fdbc4b;font-weight:bold;">while</span> (voters[to].delegate != <span style="color:#2980b9;">address</span>(<span style="color:#f67400;">0</span>)) {
            to = voters[to].delegate;

            <span style="color:#7a7c7d;">// We found a loop in the delegation, not allowed.</span>
            <span style="color:#7f8c8d;">require</span>(to != <span style="color:#7f8c8d;">msg</span>.sender, <span style="color:#f44f4f;">&quot;Found loop in delegation.&quot;</span>);
        }

        <span style="color:#7a7c7d;">// Since `sender` is a reference, this</span>
        <span style="color:#7a7c7d;">// modifies `voters[msg.sender].voted`</span>
        sender.voted = <span style="font-weight:bold;">true</span>;
        sender.delegate = to;
        Voter <span style="font-weight:bold;">storage</span> delegate_ = voters[to];
        <span style="color:#fdbc4b;font-weight:bold;">if</span> (delegate_.voted) {
            <span style="color:#7a7c7d;">// If the delegate already voted,</span>
            <span style="color:#7a7c7d;">// directly add to the number of votes</span>
            proposals[delegate_.vote].voteCount += sender.weight;
        } <span style="color:#fdbc4b;font-weight:bold;">else</span> {
            <span style="color:#7a7c7d;">// If the delegate did not vote yet,</span>
            <span style="color:#7a7c7d;">// add to her weight.</span>
            delegate_.weight += sender.weight;
        }
    }

    <span style="color:#7a7c7d;">/// Give your vote (including votes delegated to you)</span>
    <span style="color:#7a7c7d;">/// to proposal `proposals[proposal].name`.</span>
    <span style="color:#fdbc4b;font-weight:bold;">function</span> vote(<span style="color:#2980b9;">uint</span> proposal) <span style="font-weight:bold;">public</span> {
        Voter <span style="font-weight:bold;">storage</span> sender = voters[<span style="color:#7f8c8d;">msg</span>.sender];
        <span style="color:#7f8c8d;">require</span>(sender.weight != <span style="color:#f67400;">0</span>, <span style="color:#f44f4f;">&quot;Has no right to vote&quot;</span>);
        <span style="color:#7f8c8d;">require</span>(!sender.voted, <span style="color:#f44f4f;">&quot;Already voted.&quot;</span>);
        sender.voted = <span style="font-weight:bold;">true</span>;
        sender.vote = proposal;

        <span style="color:#7a7c7d;">// If `proposal` is out of the range of the array,</span>
        <span style="color:#7a7c7d;">// this will throw automatically and revert all</span>
        <span style="color:#7a7c7d;">// changes.</span>
        proposals[proposal].voteCount += sender.weight;
    }

    <span style="color:#7a7c7d;">/// </span><span style="color:#3f8058;">@dev</span><span style="color:#7a7c7d;"> Computes the winning proposal taking all</span>
    <span style="color:#7a7c7d;">/// previous votes into account.</span>
    <span style="color:#fdbc4b;font-weight:bold;">function</span> winningProposal() <span style="font-weight:bold;">public</span> <span style="font-weight:bold;">view</span>
            <span style="font-weight:bold;">returns</span> (<span style="color:#2980b9;">uint</span> winningProposal_)
    {
        <span style="color:#2980b9;">uint</span> winningVoteCount = <span style="color:#f67400;">0</span>;
        <span style="color:#fdbc4b;font-weight:bold;">for</span> (<span style="color:#2980b9;">uint</span> p = <span style="color:#f67400;">0</span>; p &lt; proposals.length; p<span style="color:#7f8c8d;">++</span>) {
            <span style="color:#fdbc4b;font-weight:bold;">if</span> (proposals[p].voteCount &gt; winningVoteCount) {
                winningVoteCount = proposals[p].voteCount;
                winningProposal_ = p;
            }
        }
    }

    <span style="color:#7a7c7d;">// Calls winningProposal() function to get the index</span>
    <span style="color:#7a7c7d;">// of the winner contained in the proposals array and then</span>
    <span style="color:#7a7c7d;">// returns the name of the winner</span>
    <span style="color:#fdbc4b;font-weight:bold;">function</span> winnerName() <span style="font-weight:bold;">public</span> <span style="font-weight:bold;">view</span>
            <span style="font-weight:bold;">returns</span> (<span style="color:#2980b9;">bytes32</span> winnerName_)
    {
        winnerName_ = proposals[winningProposal()].name;
    }
}
</pre></body></html>
